﻿#if UNITY_EDITOR
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using Unity.Collections;
/// <summary>
/// SpriteAtlas设定
/// </summary>
[XmlRoot("ESF_Xml_SpriteAtlasSetting")]
public class ESF_Xml_SpriteAtlasSetting : AbsESF_Xml
{
    /// <summary>
    /// Sprite文件目录名称
    /// </summary>
    [XmlElement("Sprite文件目录名称")]
    [ReadOnly()]
    [ESF_GUIBrower()]
    [ESF_GUISeparator()]
    public string srcSpriteAtlasFolder = "SpriteAtlas";

    /// <summary>
    /// SpriteAtlas根目录
    /// </summary>
    [XmlElement("SpriteAtlas根目录")]    
    [ESF_GUIBrower()]
    [ESF_GUISeparator()]
    [ESF_ReadOnly]
    public string[] spriteAtlasRoots;

    /// <summary>
    /// SpriteAtlas根目录
    /// </summary>
    [XmlElement("生成SpriteAtlas目标目录")]
    [ESF_GUIBrower()]
    [ESF_GUISeparator()]
    [ESF_ReadOnly]
    public string[] spriteAtlasTargets;    

    /// <summary>
    /// 生成SpriteAtlas文件目录名称
    /// </summary>
    [XmlElement("生成SpriteAtlas文件目录名称")]
    [ReadOnly()]
    [ESF_GUIBrower()]    
    [ESF_GUISeparator()]
    public string buildSpriteAtlasFolder = "SpriteAtlasAssets";

    /// <summary>
    /// 生成SpriteAtlas枚举脚本目录
    /// </summary>
    [XmlElement("生成SpriteAtlas枚举脚本目录")]
    [ReadOnly()]
    [ESF_GUIBrower()]
    [ESF_GUISeparator()]
    public string buildEnumSpriteAtlasFolder;    

    /// <summary>
    /// SpriteAtlas枚举名称
    /// </summary>
    public string enESF_SpriteAtlasName { get { return "enESF_SpriteAtlas"; } }
    /// <summary>
    /// OnResolve
    /// </summary>
    protected override void OnResolve()
    {
        List<string> roots = new List<string>();
        var abb = ESF_Logic.assetBundleBuild.xml;
        abb.Resolve();
        if (abb.supportLangs != null && abb.supportLangs.Length > 0)
        {            
            foreach (var lan in abb.supportLangs)
            {
                if (lan.assetBundleFolders != null)
                {
                    foreach (var f in lan.assetBundleFolders)
                    {
                        var path = Path.Combine(f, srcSpriteAtlasFolder).ESF_TransPathSeparatorCharToUnityChar();
                        if (!roots.Contains(path))
                        {
                            roots.Add(path);
                        }
                    }
                }                
            }
        }
        if (roots.Count > 0)
        {
            spriteAtlasRoots = new string[roots.Count];
            roots.CopyTo(spriteAtlasRoots);
        }        
        if (spriteAtlasRoots != null)
        {
            roots.Clear();
            foreach (var key in spriteAtlasRoots)
            {
                roots.Add(Path.Combine(Path.GetDirectoryName(key), buildSpriteAtlasFolder).ESF_TransPathSeparatorCharToUnityChar());
            }
        }
        spriteAtlasTargets = roots.ToArray();
        buildEnumSpriteAtlasFolder = Path.Combine(ESF_Logic.projectSetting.xml.GetCopyToXFolder(ESF_enCopyToX.CopyToHF), "SpriteAtlas/Enum").ESF_TransPathSeparatorCharToUnityChar();
        base.OnResolve();
    }
}
#endif

